﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WDTAsg2.Admin
{
    public partial class ManageDownloads : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataBindList();
            }
        }


        // Rebind data
        private void DataBindList()
        {
            HardwaresRUsDataContext db = new HardwaresRUsDataContext();
            this.lvDownloads.DataSource = db.getAllDownloads();
            this.lvDownloads.DataBind();
        }

        protected void lvDownloads_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            // Delete the item
            ListViewItem item = lvDownloads.Items[e.ItemIndex];

            // Id
            int idParam = Convert.ToInt32(((Label)item.FindControl("DownloadIdLabel")).Text);

            //Linq
            HardwaresRUsDataContext dc = new HardwaresRUsDataContext();

            //delete
            dc.deleteDownloadById(idParam);

            DataBindList();
        }

        protected void lvDownloads_ItemInserting(object sender, ListViewInsertEventArgs e)
        {
            // Insert the item
            ListViewItem item = e.Item;

            // Get fields
            string titleParam = ((TextBox)item.FindControl("TitleTextBox")).Text;
            string descriptionParam = ((TextBox)item.FindControl("DescriptionTextBox")).Text;
            int categoryIdParam = Convert.ToInt32(((DropDownList)item.FindControl("ddCategoryId")).SelectedValue);
            string downloadUrlParam = ((TextBox)item.FindControl("DownloadUrlTextBox")).Text;

            // Linq
            HardwaresRUsDataContext dc = new HardwaresRUsDataContext();
            // Insert
            dc.insertDownload(titleParam, descriptionParam, categoryIdParam, downloadUrlParam, "super");
            DataBindList();
        }

        protected void lvDownloads_ItemCanceling(object sender, ListViewCancelEventArgs e)
        {
            lvDownloads.EditIndex = -1;
            DataBindList();
        }

        protected void lvDownloads_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            // Update item

            ListViewItem item = lvDownloads.Items[e.ItemIndex];

            //Params
            int idParam = Convert.ToInt32(((Label)item.FindControl("DownloadIdLabel")).Text);
            string titleParam = ((TextBox)item.FindControl("TitleTextBox")).Text;
            string descriptionParam = ((TextBox)item.FindControl("DescriptionTextBox")).Text;
            int categoryIdParam = Convert.ToInt32(((DropDownList)item.FindControl("ddCategoryId")).SelectedValue);
            string downloadUrlParam = ((TextBox)item.FindControl("DownloadUrlTextBox")).Text;

            HardwaresRUsDataContext dc = new HardwaresRUsDataContext();

            dc.editDownloadById(idParam, titleParam, descriptionParam, categoryIdParam, downloadUrlParam);

            this.lvDownloads.EditIndex = -1;
            DataBindList();
        }

        protected void lvDownloads_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            lvDownloads.EditIndex = e.NewEditIndex;
            DataBindList();
        }
    }
}